/*
 * @Author: 没有金箍棒的齐天大圣 feng7221996@163.com
 * @Date: 2022-04-18 13:04:46
 * @LastEditors: 没有金箍棒的齐天大圣 feng7221996@163.com
 * @LastEditTime: 2022-05-05 13:24:44
 * @FilePath: \lh-qms\xy-web-lh\src\store\modules\permission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  constantRoutes,
  asyncRoutes
} from '@/router'

import Layout from '@/layout'

// const loadView = (view) => {
//   return (resolve) => require([`@/views${view}`], resolve)
// }

/**
 * According menus generate routes
 * @param meuns
 */
export function generateAsyncRoutes(meuns) {
  // console.log(meuns, '路由配置');
  // console.log(asyncRoutes,'asyncRoutes');
  // meuns.flow.forEach(menu => {
  //   console.log(menu,'ssweeerfcxdddff');
  //   const tmp = {
  //     path: menu.path,
  //     component: Layout,
  //     name: menu.resourceCode,
  //     hidden: menu.status === 0,
  //     meta: {
  //       title: menu.name,
  //       icon: menu.icon
  //     }
  //   }
    // if (menu.memo) {
    //   tmp.component = loadView(menu.memo)
    // }
    // if (menu.children) {
    //   tmp.children = generateAsyncRoutes(menu.children)
    //   let child
    //   for (child in tmp.children) {
    //     if (!child.hidden) {
    //       tmp.redirect = child.path
    //       break
    //     }
    //   }
    // }
  //   console.log(tmp,'pppppppppp');
  //   res.push(tmp)

  // })

  // console.log(this.$router);
  const res = []
  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({
    commit
  }, menus) {
    return new Promise(resolve => {
      let accessedRoutes = null
      accessedRoutes = generateAsyncRoutes(menus)

      accessedRoutes.push({
        path: '*',
        redirect: '/404',
        hidden: true
      })
      commit('SET_ROUTES', accessedRoutes)
      // console.log(accessedRoutes,' accessedRoutes');
      resolve(accessedRoutes)
    })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
